package cn.bone.gateway.news.dao;

import cn.bone.gateway.news.NewsDo;
import cn.bone.gateway.news.bean.News;
import cn.bone.gateway.news.bean.NewsDetail;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * Created by superbone on 2017/4/6.
 */
@Repository
public class NewsDao {

    @Resource
    private JdbcTemplate template;
    @Resource
    private Datastore datastore;

    public NewsDo getFirstOne(int id){
        String sql = "select * from news t where t.news_approvalstatus=3 and t.news_id > ? limit 0,1";
        List<NewsDo> list = template.query(sql, new RowMapper<NewsDo>() {

            @Override
            public NewsDo mapRow(ResultSet rs, int rowNum) throws SQLException {
                NewsDo newsDo = new NewsDo();
                News news = new News();
                news.setNewsId(rs.getInt("news_id"));
                news.setColumnId(rs.getInt("column_id"));
                news.setTitle(rs.getString("news_title"));
                news.setKeywords(rs.getString("news_keywords"));
                news.setOutLink(rs.getString("news_out_link"));
                news.setSource(rs.getString("news_source"));

                String imgStrs = rs.getString("news_listimgurl");
                if(StringUtils.isNotEmpty(imgStrs)){
                    String[] imgs = imgStrs.split(",");
                    for(String img : imgs){
                        news.getImgs().add(img);
                    }
                }
                news.setCreateTime(new Date(rs.getTimestamp("news_createtime").getTime()));
                news.setLastUpdateTime(new Date(rs.getTimestamp("news_lastupdatetime").getTime()));
                news.setRealClickNum(rs.getInt("news_realclicknum"));
                news.setVirtualClickNum(rs.getInt("news_clicknum"));
                news.setTopable(rs.getInt("news_istop")==1);
                news.setTopSort(rs.getInt("news_topsort"));
                news.setCommentable(rs.getInt("news_iscomment")==1);
                news.setType(rs.getInt("news_type"));
                news.setImgType(rs.getInt("news_liststyletype"));
                news.setState(rs.getInt("news_approvalstatus"));
                news.setCreateName(rs.getString("account_name"));

                NewsDetail detail = new NewsDetail();
                detail.setNewsId(rs.getInt("news_id"));
                detail.setContent(rs.getString("news_content"));
                newsDo.setNews(news);
                newsDo.setDetail(detail);
                return newsDo;
            }
        },id);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    public void saveNews(News news){
        datastore.save(news);
    }
    public void saveNewsDetail(NewsDetail newsDetail){
        datastore.save(newsDetail);
    }
}
